﻿using System;
using System.Collections;

namespace CSharp.Libs.Koc
{
    public static class KernelTools
    {
        #region declarations
        public enum VariableTypes
        {
            String = 1,
            Int32,
            Int64,
            Double,
            Boolean
        }
        #endregion



        #region extension methods
        /// <summary>GetHashtableValue
        /// gets a value of a Hashtable as a type given by the ReturnType parameter.</summary>
        /// <param name="HT">Hashtable to get the value of</param>
        /// <param name="Key">Name of the value to be returned</param>
        /// <param name="ReturnType">Desired return type.</param>
        /// <returns></returns>
        /// <remarks>The return type should be chosen correctly. Otherwise a default value weill be returned.</remarks>
        /// <history>created  15.05.2011</history>
        /// <history>modified 17.10.2011: put into own class as extension method</history>
        public static dynamic GetHashtableValue(
            this Kernel KRNL,
            Hashtable HT,
            String Key,
            VariableTypes ReturnType)
        {
            try
            {
                return KRNL.GetObjectValue((Object)HT[Key], ReturnType);
            }
            catch (Exception ex)
            {
                KRNL.EC.AddException(ex, KRNL.ToString(), KRNL.EC.Tools.GetCurrentMethodName());
            }

            return null;
        }

        /// <summary>GetObjectValue
        /// gets the value of the given object as a typee given by the ReturnType parameter</summary>
        /// <param name="objValue">Value to be returned</param>
        /// <param name="ReturnType">Desired return type.</param>
        /// <returns></returns>
        /// <remarks>The return type should be chosen correctly. Otherwise a default value weill be returned.</remarks>
        /// <history>created  09.06.2011</history>
        /// <history>modified 17.10.2011: put into own class as extension method</history>
        public static dynamic GetObjectValue(
            this Kernel KRNL,
            Object objValue,
            VariableTypes ReturnType)
        {
            try
            {
                switch (ReturnType)
                {
                    case VariableTypes.String:
                        if (objValue == null)
                        {
                            return "";
                        }
                        return objValue.ToString();

                    case VariableTypes.Double:
                        if (objValue == null)
                        {
                            return (Double)0;
                        }
                        Double dblRetVal = 0;
                        Double.TryParse(objValue.ToString(), out dblRetVal);
                        return dblRetVal;

                    case VariableTypes.Int32:
                        if (objValue == null)
                        {
                            return (Int32)0;
                        }
                        Int32 int32RetVal = 0;
                        Int32.TryParse(objValue.ToString(), out int32RetVal);
                        return int32RetVal;

                    case VariableTypes.Int64:
                        if (objValue == null)
                        {
                            return (Int64)0;
                        }
                        Int64 int64RetVal = 0;
                        Int64.TryParse(objValue.ToString(), out int64RetVal);
                        return int64RetVal;

                    case VariableTypes.Boolean:
                        if (objValue == null)
                        {
                            return false;
                        }
                        Boolean fRetVal = false;
                        Boolean.TryParse(objValue.ToString(), out fRetVal);
                        return fRetVal;
                }
            }
            catch (Exception ex)
            {
                KRNL.EC.AddException(ex, KRNL.ToString(), KRNL.EC.Tools.GetCurrentMethodName());
            }

            return null;
        }

        /// <summary>ReturnHashtableOk
        /// returns the correct bool for the Hashtable ok value.</summary>
        /// <param name="htReturn">Hashtable with ok key defined in constants.KEY_OK</param>
        /// <returns>true if the value of the key defined in constants.KEY_OK exists and is true, else returns false</returns>
        /// <history>created  04.07.2011</history>
        /// <history>modified 17.10.2011: put into own class as extension method</history>
        /// <history>modified 22.04.2012 If no "ok" is in Hashtable, return true because it is probably ok then</history>
        public static Boolean ReturnHashtableOk(
            this Kernel KRNL,
            Hashtable htReturn)
        {
            try
            {
                if (htReturn != null)
                {
                    if (htReturn[constants.KEY_OK] != null)
                    {
                        if (htReturn[constants.KEY_OK] is Boolean)
                        {
                            return (Boolean)htReturn[constants.KEY_OK];
                        }
                        else if (htReturn[constants.KEY_OK] is String)
                        {
                            return Boolean.Parse(htReturn[constants.KEY_OK].ToString());
                        }
                    }
                    else
                    {
                        //22.04.2012 If no "ok" is in Hashtable, return true.
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                KRNL.EC.AddException(ex, KRNL.ToString(), KRNL.EC.Tools.GetCurrentMethodName());
            }

            return false;
        }
        #endregion
    }
}
